//
//  ResultViewController.m
//  Weiweiyunshi
//
//  Created by 王译 on 17/2/5.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "ResultViewController.h"
#import "ResultView.h"
#import "LoginModel.h"
#import "AddressViewController.h"
#define URL @"http://210.72.239.121:8088/SSIL/UserInformation.do?userId=%@"
@interface ResultViewController ()
{
    NSDictionary *dic;
    ResultView *resuView;
}
@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"详细信息";
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_back_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = backItem;
    [self qrCodeSearch];
//    resuView = [[ResultView alloc]init];
//    self.view = resuView;
//    NSLog(@"%@",self.isFriend);
//   /* if ([self.isFriend isEqualToString:@"1"]) {
//        [resuView.addButton setTitle:@"对方已是您的好友" forState:UIControlStateNormal];
//    }else{*/
//        [resuView.addButton addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}
-(void)pop
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
    
}
-(void)addFriend:(id)sender{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userId =  [userDefaults objectForKey:@"loginName"];
    NSString *addUrl = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/MyFriend.do?style=add&userId=%@&friendId=%@",userId,self.userId];
    [WYHttpRequest getWithURLString:addUrl parameters:nil success:^(id responseObject) {
        if ([responseObject[@"Code"]isEqualToString:@"1"]) {
            [MBProgressHUD showSuccessMessage:@"添加成功"];
        }else{
            [MBProgressHUD showErrorMessage:@"添加失败"];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)qrCodeSearch{
    NSString *urlStr = [NSString stringWithFormat:URL,self.userId];
    NSLog(@"%@",urlStr);
    [WYHttpRequest getWithURLString:urlStr parameters:nil success:^(id responseObject) {
        
        dic = responseObject;
        if (dic.count == 0 ) {
//            [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [MBProgressHUD showErrorMessage:@"用户数据不存在"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            resuView = [[ResultView alloc]init];
            self.view = resuView;
            NSLog(@"%@",self.isFriend);
            /* if ([self.isFriend isEqualToString:@"1"]) {
             [resuView.addButton setTitle:@"对方已是您的好友" forState:UIControlStateNormal];
             }else{*/
            [resuView.addButton addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
            
        [resuView.imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"img"]]];
        resuView.nameLabel.text = dic[@"username"];
        resuView.mobileLabel.text = dic[@"mobile"];
        resuView.emailLabel.text = dic[@"email"];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
